package com.iseastar.dianxiaosan.home.parcel;

import android.os.Bundle;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ListView;
import android.widget.TextView;

import com.iseastar.BaseActivity2;
import com.iseastar.dianxiaosan.home.adapter.ParcelCheckAdapter;
import com.iseastar.dianxiaosan.model.ParcelCheckBean;
import com.iseastar.dianxiaosan.model.ParcelCheckResultBean;
import com.iseastar.guojiang.consts.MsgID;
import com.iseastar.guojiang.logic.AppHttp;
import com.iseastar.guojiang.logic.JSON;
import com.iseastar.guojiang.model.ReqResult;
import com.shouhuobao.station.R;

import java.util.ArrayList;
import java.util.HashSet;

import droid.frame.ui.dialog.AppDialog;

/**
 * 店老板 核对(通过扫描条形码or寄件码)包裹信息
 *
 * @author HuXinDan<br>
 *         2016-11-17下午3:32:12
 */
public class ParcelCheckActivity extends BaseActivity2 implements OnClickListener {

    private ListView mListView;
    private ParcelCheckAdapter mAdapter;
    private TextView mWaitNum;// 待收取的包裹数量
    private TextView mCheckAlls;// 全部收取

    private ArrayList<ParcelCheckBean> otherParcel;
    /**
     * 1、通过6位寄件码收包裹<br>
     * 0、通过袋子条形码收包裹
     */
    private int type;

    protected void onCreate(Bundle savedInstanceState) {
        type = getIntent().getIntExtra("type", 0);
        super.onCreate(savedInstanceState);
        if (type == 1) {
            String code = getIntent().getStringExtra("code");
            AppHttp.getInstance().stationParcelByCode(code);
        } else {
            // 条形码收包裹
            String bagCode = getIntent().getStringExtra("bagCode");
            AppHttp.getInstance().stationParcelCheckAlls(bagCode);
        }
    }

    protected void findViewById() {
        setContentView(R.layout.parcel_check);
        super.findViewById();
        getAppTitle().setCommonTitle("核对包裹信息");
        mWaitNum = (TextView) findViewById(R.id.parcel_check_wait_num);
        mCheckAlls = (TextView) findViewById(R.id.parcel_check_alls);
        mCheckAlls.setOnClickListener(this);
        mListView = (ListView) findViewById(R.id.parcel_check);
    }

    @Override
    public boolean handleMessage(Message msg) {
        switch (msg.what) {
            // 获取驿站点下面的订单列表（核对包裹）
            case MsgID.parcel_by_bagCode:
                // 通过6位寄件码收包裹
            case MsgID.parcel_by_6code:
                onRefreshComplete();
                final ReqResult<ParcelCheckResultBean> result = JSON.parser(msg.obj, ParcelCheckResultBean.class);
                if (checkLoginStatus(result) && result.getResult() != null && result.getResult().getIsEffect() != 0) {
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            ParcelCheckResultBean items = result.getResult();
                            ParcelCheckBean currentParcel = items.getCurrentParcel();// 当前的包裹（对象）
                            otherParcel = items.getOtherParcel();
                            otherParcel.add(0, currentParcel);
                            mAdapter = new ParcelCheckAdapter(otherParcel, (ParcelCheckActivity) getContext(), mListView);
                            mListView.setAdapter(mAdapter);
                            // 一个包裹隐藏
                            // if (otherParcel.size() == 1) {
                            // findViewById(R.id.parcel_check_one_layout).setVisibility(View.GONE);
                            // }
                            mWaitNum.setText("共" + otherParcel.size() + "个包裹待收取");
                        }
                    });
                } else if (result.getResult() != null && result.getResult().getIsEffect() == 0) {
                    if (type == 1) {
                        showToast("寄件码无效,请重试!");
                    } else {
                        showToast("投递袋编码无效,请重试!");
                    }
                    finish();
                } else {
                    showToast(result.getMessage());
                }
                return true;
            // 通过6位数寄件码查询包裹，然后收取包裹
            case MsgID.parcel_recv_by_ids:
                cancelLoadingDialog();
                ReqResult<?> result2 = JSON.parser(msg.obj);
                if (checkLoginStatus(result2)) {
                    showToast("收取成功");
                    int id = msg.arg1;// 收取的包裹id
                    if (id != 0) {// 收取单个包裹
                        for (ParcelCheckBean item : mAdapter.getItems()) {
                            if (item.getId() == id) {
                                item.setIsTake(1);
                            }
                        }
                    } else {// 收取所有包裹
                        for (ParcelCheckBean item : mAdapter.getItems()) {
                            item.setIsTake(1);
                        }
                    }
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            HashSet<Integer> idsSet = mAdapter.getUnTakedSet();
                            mWaitNum.setText("共" + idsSet.size() + "个包裹待收取");
                            // 如果都收取的话 就不显示了
                            if (idsSet.isEmpty()) {
                                findViewById(R.id.parcel_check_one_layout).setVisibility(View.GONE);
                            }
                            mAdapter.notifyDataSetChanged();
                        }
                    });
                } else {
                    showToast(result2.getMessage());
                }
                return false;
            default:
                return super.handleMessage(msg);
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.parcel_check_alls:
                if (mAdapter == null || mAdapter.getUnTakedSet() == null || mAdapter.getUnTakedSet().size() == 0) {
                    return;
                }
                final HashSet<Integer> idsSet = mAdapter.getUnTakedSet();
                final AppDialog dialog = new AppDialog(this);
                dialog.setContent("确定收取" + idsSet.size() + "个包裹?");
                dialog.setCancelClickListener("取消", null);
                dialog.setConfirmClickListener("确定", new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        dialog.dismiss();
                        showLoadingDialog(null);
                        String ids = idsSet.toString().replace("[", "").replace("]", "").replace(" ", "");
                        AppHttp.getInstance().stationReceiveByIds(ids);
                    }
                });
                dialog.show();
                break;
        }
    }

}
